package com.hundsun.s3.common.util;

/**
 * com.hundsun.s3.platform.pub.utils
 *
 * @Description:读取properties文件工具类
 * @Author: zhanglei23613
 * @Date: Created in 9:50 2018/2/8 0008
 * V1.0 9:50 2018/2/8 0008
 */

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public final class PropsUtils {


    public static Properties loadProps(String fileName) {
        /**
         * @Description:加载属性文件
         * @param: [fileName]
         * @return: java.util.Properties
         * @Author: zhanglei23613
         * @Date: 10:05 2018/2/8 0008
         * V1.0
         */

        Properties props = null;
        InputStream is = null;
        try {
            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            if (is == null) {
                throw new FileNotFoundException(fileName + " file is not found");
            }
            props = new Properties();
            props.load(is);
        } catch (IOException e) {
            //LOGGER.error("load properties file failure", e);
            //打印错误日志
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
//                    LOGGER.error("close input stream failure", e);
                    //打印错误日志
                }
            }
        }
        return props;
    }


    public static String getString(Properties props, String key) {
        /**
         * @Description:获取字符属性（无默认值）
         * @param: [props, key]
         * @return: java.lang.String
         * @Author: zhanglei23613
         * @Date: 10:05 2018/2/8 0008
         * V1.0
         */

        return getString(props, key, "");
    }


    public static String getString(Properties props, String key, String defaultValue) {
        /**
         * @Description:获取字符属性（可指定默认值）
         * @param: [props, key, defaultValue]
         * @return: java.lang.String
         * @Author: zhanglei23613
         * @Date: 10:06 2018/2/8 0008
         * V1.0
         */

        String value = defaultValue;
        if (props.containsKey(key)) {
            value = props.getProperty(key);
        }
        return value;
    }

}